"use strict";
const common_vendor = require("../../common/vendor.js");
const common_api = require("../../common/api.js");
const common_const = require("../../common/const.js");
require("../../common/request.js");
const tabBar = () => "../../components/tab-bar/tab-bar.js";
const _sfc_main = {
  components: {
    tabBar
  },
  data() {
    return {
      user: {
        info: {
          avatar: ""
        }
      },
      systemInfo: {},
      urlPrefix: common_const.urlPrefix,
      myText: "我的",
      list: [
        {
          "name": "退出登录",
          "page": ""
        },
        {
          "name": "修改个人资料",
          "page": "/pages/profile/profile"
        },
        {
          "name": "隐私条款与服务协议",
          "page": "/pages/agreement/agreement"
        },
        {
          "name": "问题反馈或意见建议",
          "page": "/pages/feedback/feedback"
        }
      ]
    };
  },
  watch: {
    user: {
      handler(newUser, oldUser) {
        console.log(oldUser);
        console.log(newUser);
        this.setUserInfo();
      },
      deep: true
    }
  },
  onLoad() {
    this.systemInfo = common_vendor.index.getSystemInfoSync();
    this.systemInfo.titleBarHeight = (common_vendor.index.getMenuButtonBoundingClientRect().top - this.systemInfo.statusBarHeight) * 2 + common_vendor.index.getMenuButtonBoundingClientRect().height;
    this.loadUserInfo();
  },
  onPullDownRefresh() {
    this.getUserInfo();
    setTimeout(() => {
      common_vendor.index.stopPullDownRefresh();
    }, 200);
  },
  methods: {
    loadUserInfo() {
      let user = common_vendor.index.getStorageSync("user");
      if (user && user.info && user.id) {
        this.user = user;
        console.log(this.user.info);
      } else {
        common_vendor.index.reLaunch({
          url: "/pages/login/login",
          success: () => {
            common_vendor.index.showToast({
              title: "用户信息不存在\n请重新登录",
              icon: "none"
            });
          }
        });
      }
    },
    setUserInfo() {
      common_vendor.index.setStorage({
        key: "user",
        data: this.user
      }).then(() => {
        console.log("存储用户数据成功");
      }).catch(() => {
        console.log("存储用户数据失败");
      });
    },
    getUserInfo() {
      common_api.getVisitorProfile_id(this.user.id).then((res) => {
        this.user.info = res.data;
      });
    },
    gotoPage(page) {
      if (!page) {
        common_vendor.index.showModal({
          content: "请问是否确定退出登录",
          success(res) {
            if (res.confirm) {
              common_vendor.index.clearStorageSync();
              common_vendor.index.showLoading({
                title: "退出中\n请重新登录"
              });
              setTimeout(() => {
                common_vendor.index.reLaunch({
                  url: "/pages/login/login",
                  success: () => {
                    common_vendor.index.hideLoading();
                    common_vendor.index.showToast({
                      title: "已返回登录页面",
                      icon: "none"
                    });
                  }
                });
              }, 500);
            }
          }
        });
        return;
      }
      if (page == "/pages/profile/profile") {
        page = page + "?user=" + JSON.stringify(this.user);
      } else if (page == "/pages/feedback/feedback") {
        page = page + "?user=" + JSON.stringify(this.user);
      }
      common_vendor.index.navigateTo({
        url: page
      });
    },
    uploadAvatar() {
      common_vendor.index.chooseImage({
        count: 1,
        success: (res) => {
          const tempFilePaths = res.tempFilePaths;
          common_vendor.index.showLoading({
            title: "头像上传中"
          });
          common_vendor.index.uploadFile({
            url: "https://mock.apifox.cn/m1/2680723-0-default/avatar",
            filePath: tempFilePaths[0],
            name: "avatar",
            success: (uploadFileRes) => {
              let res2 = JSON.parse(uploadFileRes.data);
              this.user.info.avatar = res2.data.url;
              this.setUserInfo();
              common_vendor.index.showToast({
                title: "上传成功",
                icon: "success"
              });
            },
            fail: () => {
              common_vendor.index.showToast({
                title: "上传失败",
                icon: "error"
              });
            },
            complete: () => {
              common_vendor.index.hideLoading();
            }
          });
        }
      });
    }
  }
};
if (!Array) {
  const _easycom_tab_bar2 = common_vendor.resolveComponent("tab-bar");
  _easycom_tab_bar2();
}
const _easycom_tab_bar = () => "../../components/tab-bar/tab-bar.js";
if (!Math) {
  _easycom_tab_bar();
}
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  return {
    a: $data.urlPrefix + "/img/my/background.png",
    b: common_vendor.s("height:" + $data.systemInfo.titleBarHeight + "px;"),
    c: $data.user.info.avatar,
    d: common_vendor.o((...args) => $options.uploadAvatar && $options.uploadAvatar(...args)),
    e: common_vendor.t($data.user.info.userName),
    f: common_vendor.f($data.list, (item, index, i0) => {
      return common_vendor.e({
        a: common_vendor.t(item.name),
        b: item.page
      }, item.page ? {} : {}, {
        c: common_vendor.o(($event) => $options.gotoPage(item.page), index),
        d: index
      });
    }),
    g: common_vendor.s("padding-top:" + $data.systemInfo.statusBarHeight + "px;"),
    h: common_vendor.p({
      activeIndex: "2"
    }),
    i: common_vendor.s("height:" + $data.systemInfo.windowHeight + "px !important;")
  };
}
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-2f1ef635"], ["__file", "D:/Project/heart-3.0/heart-field-3.0/pages/my/my.vue"]]);
wx.createPage(MiniProgramPage);
